package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.service.BaseManageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@SuppressWarnings("all")
public class BaseManageServiceImpl implements BaseManageService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseAttrInfoMapper baseAttrInfoMapper;

    @Autowired
    private BaseAttrValueMapper baseAttrValueMapper;
    //获取一级分类数据
    @Override
    public List<BaseCategory1> getCategory1() {
        return baseCategory1Mapper.selectList(null);
    }

    //获取二级分类数据
    @Override
    public List<BaseCategory2> getCategory2(Long category1Id) {
        QueryWrapper<BaseCategory2> category2Query = new QueryWrapper<BaseCategory2>().eq("category1_id", category1Id);
        List<BaseCategory2> category2List = baseCategory2Mapper.selectList(category2Query);
        return category2List;
    }

    //获取三级分类数据
    @Override
    public List<BaseCategory3> getCategory3(Long category2Id) {
        QueryWrapper<BaseCategory3> category3Query = new QueryWrapper<BaseCategory3>().eq("category2_id", category2Id);
        List<BaseCategory3> category3List = baseCategory3Mapper.selectList(category3Query);
        return category3List;
    }

    //根据分类id获取平台属性集合
    @Override
    public List<BaseAttrInfo> attrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        return baseAttrInfoMapper.SelectAttrInfoList(category1Id,category2Id,category3Id);
    }

    //保存/修改平台属性
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        //判断什么时候更新,什么时候新增
        if(baseAttrInfo.getId()!=null){
            //查询回显后进行更新
            baseAttrInfoMapper.updateById(baseAttrInfo);
        }else {
            //新增
            baseAttrInfoMapper.insert(baseAttrInfo);
        }
        //操作完BaseAttrInfo后操作BaseAttrValue
        //BaseAttrInfo类中带有BaseAttrValue的List集合
        //通过BaseAttrInfo的id删除所有BaseAttrValue之后在把所有数据加进去
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        if(attrValueList != null && attrValueList.size()>0){
            //封装条件
            QueryWrapper<BaseAttrValue> attrIdQuery = new QueryWrapper<BaseAttrValue>().eq("attr_id", baseAttrInfo.getId());
            baseAttrValueMapper.delete(attrIdQuery);
            //删除之后重新增加所有数据
            for (BaseAttrValue baseAttrValue : attrValueList) {
                //关联每条数据的id
                baseAttrValue.setAttrId(baseAttrInfo.getId());
                //一条一条往里边加
                baseAttrValueMapper.insert(baseAttrValue);
            }
        }
    }

    //修改按钮传回attrId进行数据回显
    /*@Override
    public List<BaseAttrValue> getAttrValueList(Long attrId) {
        //封装条件
        QueryWrapper<BaseAttrValue> attrIdQuery = new QueryWrapper<BaseAttrValue>().eq("attr_id", attrId);
        List<BaseAttrValue> attrValueList = baseAttrValueMapper.selectList(attrIdQuery);
        return attrValueList;
    }*/
    //修改按钮传回attrId进行数据回显的升级版,代码体验
    @Override
    public BaseAttrInfo getAttrInfo(Long attrId) {
        BaseAttrInfo baseAttrInfo = baseAttrInfoMapper.selectById(attrId);
        baseAttrInfo.setAttrValueList(this.getAttrValueList(attrId));
        return baseAttrInfo;
    }

    //根据attrId软删除属性
    @Override
    public void deleteAttrInfoById(Long attrId) {
        baseAttrInfoMapper.deleteById(attrId);
    }

    public List<BaseAttrValue> getAttrValueList(Long attrId) {
        //封装条件
        QueryWrapper<BaseAttrValue> attrIdQuery = new QueryWrapper<BaseAttrValue>().eq("attr_id", attrId);
        List<BaseAttrValue> attrValueList = baseAttrValueMapper.selectList(attrIdQuery);
        return attrValueList;
    }


}
